package com.example.t1;

import android.Manifest;
import android.app.Activity;
import android.app.LoaderManager;
import android.content.ContentValues;
import android.content.CursorLoader;
import android.content.Loader;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.Build;
import android.provider.ContactsContract;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

public class MainActivity extends AppCompatActivity {

    private CurserLoadCallback callback;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.tv_hello).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ToneSqlHelper helper = new ToneSqlHelper(MainActivity.this, "shuangge", null, 1);
                SQLiteDatabase writableDatabase = helper.getWritableDatabase();
                ContentValues contentValues = new ContentValues();
                contentValues.put("name", "fushuang");
                contentValues.put("address", "fushuang666");
                writableDatabase.insert(ToneSqlHelper.TABLE_NAME, null, contentValues);
            }
        });
        if (Build.VERSION.SDK_INT >= 23) {
            if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
                requestPermissions(new String[]{Manifest.permission.READ_CONTACTS}, 1);
            }
        }

        SharedPreferences t1 = getSharedPreferences("t1", MODE_PRIVATE);
        SharedPreferences.Editor edit = t1.edit();
        edit.putString("haha", "fs");
        edit.apply();

        ToneSqlHelper helper = new ToneSqlHelper(this, "shuangge", null, 1);
        SQLiteDatabase writableDatabase = helper.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put("name", "fushuang");
        contentValues.put("address", "fushuang666");
        writableDatabase.insert(ToneSqlHelper.TABLE_NAME, null, contentValues);


        callback = new CurserLoadCallback(this);
        getLoaderManager().initLoader(0, null, callback);
    }

    public static class CurserLoadCallback implements LoaderManager.LoaderCallbacks<Cursor> {

        private final Activity activity;
        public String TAG = "fs666";
        static final String[] CONTACTS_SUMMARY_PROJECTION = new String[]{
                ContactsContract.Contacts._ID,
                ContactsContract.Contacts.DISPLAY_NAME,
                ContactsContract.Contacts.LAST_TIME_CONTACTED,
                ContactsContract.Contacts.LOOKUP_KEY,
        };

        public CurserLoadCallback(Activity activity) {
            this.activity = activity;
        }

        @Override
        public Loader<Cursor> onCreateLoader(int id, Bundle args) {
            Uri baseUri = null;
            baseUri = Uri.parse("content://" + BuildConfig.APPLICATION_ID +
                    ".db.DataContentProvider");
            String select = "((" + ContactsContract.Contacts.DISPLAY_NAME + " NOTNULL) AND ("
                    + ContactsContract.Contacts.HAS_PHONE_NUMBER + "=1) AND ("
                    + ContactsContract.Contacts.DISPLAY_NAME + " != '' ))";


            return new CursorLoader(activity,
                    baseUri,
                    null, null, null, null);
        }

        @Override
        public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
            data.moveToFirst();
            do {
                int columnCount = data.getColumnCount();
                String columnName = data.getColumnName(1);
                String string = data.getString(1);
                Log.d(TAG, "onLoadFinished: " + columnName + " columnCount " + columnCount + "string" + string);
            } while (data.moveToNext());
        }

        @Override
        public void onLoaderReset(Loader<Cursor> loader) {

        }
    }

}
